 var oEditors = [];
  
  $(function() {
    nhn.husky.EZCreator.createInIFrame({
      oAppRef: oEditors,
      elPlaceHolder: "textarea1",
      sSkinURI: "../se2/SmartEditor2Skin.html",        
      fCreator: "createSEditor2"
  });

  });
  
  function add(){
  	if($('.add').css("display")=='none')
  		$('.add').show();
  	else
  		$('.add').hide();
  	
  }
  function add_exit(){
  	$('.add').hide();
  }
  function file_add(){
		$('#file_add').append('<div><div class="span2"></div><input type="file" name="file" id="notice_file"/><a onclick="$(this).parent().remove();">x</a></div>');
		$('#files_add').append('<div><div class="span2"></div><input type="file" name="file" id="notice_file"/><a onclick="$(this).parent().remove();">x</a></div>');
	}
  
  function file_remove(){
	  $('input[name=file]').parent().remove();
  }
  
  
 var myModule = angular.module('myModule', []);
 myModule.filter('range', function() {
	  return function(input, total) {
	    total = parseInt(total);
	    for (var i=0; i<total; i++)
	      input.push(i);
	    return input;
	  };
});

 myModule.controller('MyController', function ($scope,$http,$location) {
     
	 $scope.user={};
	 $scope.contest_reg_id=$location.search().contest_reg_id;
	 $scope.liItems=[];
	 $scope.contest_reg_file=[];
	$scope.selectedLi={};
	$scope.contest_reg={}; 
	$scope.join=null;
	$scope.track=[];
	$scope.reportList=[];
	$scope.contest_board=[];
	$scope.notice_ck=0;
	$scope.id=null;
	$scope.times=3;
	$scope.schedule=[];
	$scope.showReply2=false;
	
	$scope.selectedTeam={};
	
	$scope.teamList=[];
	$scope.list=function(){
		$scope.selectedTeam={};
		 $scope.showReply2=false;
		$scope.content_id=null;
		$scope.contentCheck=true;
		
	}
	$scope.timesAdd=function(){
		$scope.times+=1;
	}
	
$scope.submit=function(){
    	
    	oEditors.getById["textarea1"].exec("UPDATE_CONTENTS_FIELD", []);
    }

$scope.submit2=function(){
	oEditors.getById["textarea2"].exec("UPDATE_CONTENTS_FIELD", []);
}


	$scope.showTeamReply2=function(index){
		$scope.selectedTeam=$scope.teamList[index].team;
		var team_id=$scope.teamList[index].team.team_id;
		$scope.showReply2=true;
		myUtil.onHttp($http,'get','reply2list.do',{content_id : $scope.content_id,team_id:team_id},null,function(data){
			$scope.reply2=data.reply2;
			$scope.files=data.files;
		})
		
		myUtil.onHttp($http,'get','content3.do',{content_id:$scope.content_id,team_id:team_id},null,function(data){
    			 		$scope.teamfiles=data;
    	})
	}

	$scope.doownload=function(id){
		location.href="fileDownload.do?id="+id;
	}
	$scope.modalShow =function(){
		$(".modal").modal('show');
	}
	
	$scope.modalHide=function(){
		$('.modal').modal('hide');
	}
	
	$scope.r_modify=-1;
	$scope.r_value=null;
	
	
	$scope.currentPageitems = [];
    $scope.currentPage = 1;
    $scope.pages = [];
    $scope.pagePrev = 0;
    $scope.pageNext = 0;
    $scope.pageSize = 5;
    
    $scope.contest_board_id=null;
    $scope.content_id=$location.search().content_id;
    $scope.content={};
    $scope.reply2={};
    $scope.files={};
    $scope.team=[];
    $scope.writeReply2={};
    $scope.modifyReply2={};
    $scope.selectedIndex=null;
    $scope.modifyReply2content=null;
    
    $scope.contentCheck=true;
    
    $scope.teamlist=function(){
    	myUtil.onHttp($http,'get','teamList.do',{contest_reg_id : $scope.contest_reg_id},null,function(data){

			 $scope.teamList=data;
		})
    }
    
	 $scope.select = function(item){
		 $scope.init();
		 $scope.selectedLi=item;
		 
		 if($scope.selectedLi.href=='#contest_list'){
			 $scope.teamlist();
			
		 }
		 else if($scope.selectedLi.href=='#contest_report'){
			 myUtil.onHttp($http,'get','reportList.do',{contest_reg_id:$scope.contest_reg_id},null,function(data){
				 $scope.reportList=data;
			 })
		 }
		 else if($scope.selectedLi.href=="#contest_board"){
			 myUtil.onHttp($http,'get','contest_board.do',{contest_reg_id:$scope.contest_reg_id},null,function(data){
				 $('#contest_board').show();
				 $scope.contest_board=data.content;
				 $scope.contest_board_id=data.board.board_id;
				 $scope.gotoPage($scope.currentPage);
				 
			 })
		 }
		 else if($scope.selectedLi.href=="#contest_team_board"){
			 myUtil.onHttp($http,'get','team_board.do',{team_id:$scope.team_id},null,function(data){
				 $('#team_board').show();
				 $scope.contest_board=data.content;
				 $scope.contest_board_id=data.board.board_id;
				 $scope.gotoPage($scope.currentPage);
				 
				 if($scope.countWrite!=1){
				 nhn.husky.EZCreator.createInIFrame({
				      oAppRef: oEditors,
				      elPlaceHolder: "textarea1",
				      sSkinURI: "../se2/SmartEditor2Skin.html",        
				      fCreator: "createSEditor2"
				    	  
				 })
				 $scope.countWrite=1;
				 }
			 })
		 }


	 }
	 $scope.countWrite=0;
	 $scope.show=false;
	 $scope.writeShow=function(){
		 $scope.show=true;
	 }
	 
	 $scope.init=function(){
	
		 file_remove();
		 if($scope.showWrite){
			 oEditors.getById['textarea1'].exec('UPDATE_CONTENTS_FIELD', []);
		 }
		
		 $scope.showModify=false;
		 $scope.selectedIndex=-1;
		 $scope.selectedTeam={};
		 $scope.showReply2=false;
		 $scope.contentCheck=true;
		 $scope.content_id=null;
		 $('#contest_team_board_edit').hide();
		 $('#contest_board_edit').hide();
		 $('#contest_board_modify').hide();
	 }
	 
	
     
     $scope.gotoPage = function(page) {
         $scope.currentPage = page;
     	var pageCount= Math.ceil($scope.contest_board.length * 1.0 / $scope.pageSize);
     	var pageBase = Math.floor(($scope.currentPage - 1) / 10) * 10;
     	$scope.pages = [];
         for (var i = 1; i <= 10 && pageBase + i <= pageCount; ++i)
             $scope.pages.push(pageBase + i);
         if (pageBase > 0) $scope.pagePrev = pageBase; else delete $scope.pagePrev;
         if (pageBase + 11 < pageCount) $scope.pageNext = pageBase + 11; else delete $scope.pageNext;

     	$scope.currentPageitems = [];
     	for (var i = 0; i < $scope.pageSize; ++i) {
     		var n = ($scope.currentPage - 1) * $scope.pageSize + i;
     		if (n >= $scope.contest_board.length) break;
     		$scope.currentPageitems.push($scope.contest_board[n]);    		
     	}
     } 
	 
	
	 
	 $scope.isActive = function(item){
		 return $scope.selectedLi===item;
	 }
	 
	 $scope.contest_reg_fileDownload=function(id){
		 location.href="contest_reg_fileDownload.do?id="+id;
	 }
	 
	 $scope.reply2Write=function(){
		 $scope.writeReply2.type=($scope.selectedLi.href=="#contest_team_board" ? "댓글":"첨부글");
		 $scope.writeReply2.team_id=$scope.team_id==null? $scope.selectedTeam.team_id: $scope.team_id;
		 $scope.writeReply2.content=$scope.writeReply2content;
		 $scope.writeReply2.content_id=$scope.content_id;
		 $scope.writeReply2.id=$scope.user.id;
		 $scope.writeReply2content=null;
		 
		 
		 myUtil.onHttp($http,'post','reply2Write.do',null, $scope.writeReply2,function(data){
			 $scope.reply2.push(data);
		 })
	 }
	 
	 $scope.download=function(id){
		 location.href="fileDownload.do?id="+id;
	 }
	 $scope.fileDelete=function(index){
		 if(confirm("정말 파일을 삭제하시겠습니까?")){
			 myUtil.onHttp($http,'get','fileDelete.do',{file_id:$scope.teamfiles[index].file_id}, null,function(data){
				if(data=='false'){
					alert("삭제실패");				
				}
				else{
					$scope.teamfiles.splice(index,1);
				}
			 })
		 }
		 
	 }
	 
	 $scope.m_reply2 =function(index,content){
   	  $scope.selectedIndex=index;
   	  $scope.modifyReply2content=content;
     }
	 
	 $scope.initReply2=function(){
   	  $scope.selectedIndex=-1;
     }
	 
	 $scope.reply2Modify=function(index){
		 $scope.modifyReply2.content=$scope.modifyReply2content;
		 $scope.modifyReply2.reply2_id=$scope.reply2[index].reply2_id;
		 $scope.writeReply2content=null;
   	  	myUtil.onHttp($http, 'post', 'reply2Modify.do',null,$scope.modifyReply2,
   	  			function(data){
   	  				$scope.reply2[index]=data; $scope.modifyReplycontent=''; $scope.selectedIndex=-1;
   	  			});
		 
	 }
	 
	 $scope.reply2Delete = function(index){
   	  if(confirm("댓글을 삭제하시겠습니까?")){
   	  myUtil.onHttp($http, 'post', 'reply2Delete.do',{reply2_id : $scope.reply2[index].reply2_id ,content_id: $scope.content_id},null,function(){$scope.reply2.splice(index,1);});
   	  }
     }
	 
     $scope.loadData = function() {
    	
    	 
     	myUtil.onHttp($http,'get','contest_reg.do',{id :  $scope.contest_reg_id},null,
     			function(data){
     				$scope.track=data.track;
     				$scope.user=data.user;
     				$scope.contest_reg=data.contest_reg;
     				$scope.join=data.join;
     				$scope.contest_reg_file=data.contest_reg_file;
     				$scope.schedule=data.schedule;
     				
     				if($scope.join==false){
     					if(!$scope.user || $scope.user.authority=="ROLE_STUDENT"){ // 비회원이거나 참여팀이 없을 때 메뉴 생성
     					 $scope.liItems=[{name:"대회 일정 및 상세 내용",href:"#contest_schedule",notice:"대회의 일정과 정보를 보여줍니다."},
    	                 {name:"참가팀 목록",href:"#contest_list",notice:"대회에 참여하고있는 팀을 보여줍니다."},
    	                 {name:"시상결과",href:"#contest_score",notice:"대회의 시상결과를 보여줍니다."}];
     					 
     					 
     					}
     					
     					else if($scope.user.authority=="ROLE_ADMIN"){ // 관리자일때 메뉴 생성
         					 $scope.liItems=[{name:"자료제출 및 공지", href:"#contest_board",notice:"대회의 게시판입니다.(자료제출,공지)"},
        	                 {name:"대회 일정 및 상세 내용",href:"#contest_schedule",notice:"대회의 일정과 정보를 보여줍니다."},
        	                 {name:"참가팀 목록",href:"#contest_list",notice:"대회에 참여하고있는 팀을 보여줍니다."},
        	                 {name:"필수산출물관리",href:"#contest_report",notice:"팀들이 최종적으로 내야하는 제출물을 관리합니다."},
        	                 {name:"대회시상 및 시상결과",href:"#contest_score",notice:"대회시상을 하는 페이지입니다."}];
         					 $scope.id=$scope.user.id;
         					 
        	                
         				}
     					$scope.select($scope.liItems[0]);
     					if($location.search().content_id){
        			   		 $scope.contest_board_board($location.search().content_id);
        			    }
     					
     				}
     				
     				else{
     					
     					myUtil.onHttp($http,'get','myTeamList.do',{id:$scope.user.id,contest_reg_id:$scope.contest_reg_id},null,function(data)
     							{
     						$scope.team=data;
     						$scope.team_id=$scope.team[0].team_id ; 
     						$scope.id=$scope.user.id;
     						if($location.search().content_id){
           			   		 	$scope.contest_board_board($location.search().content_id);
     						}
     						$scope.liItems=[{name:"자료제출 및 공지", href:"#contest_board",notice:"대회의 게시판입니다.(자료제출,공지)"},
        			    	                 {name:"대회 일정 및 상세 내용",href:"#contest_schedule",notice:"대회의 일정과 정보를 보여줍니다."},
        			    	                 {name:"팀 게시판",href:"#contest_team_board",notice:"팀별로 이용할 수 잇는 게시판입니다."},
        			    	                 {name:"팀원 및 제안서 관리",href:"#contest_setting",notice:"팀원의 변경과 제안서를 관리하는 페이지입니다."},
        			    	                 {name:"참가팀 목록",href:"#contest_list",notice:"대회에 참여하고있는 팀을 보여줍니다."},
        			    	                 {name:"최종 산출물 제출",href:"#contest_finalReport",notice:"최종적으로 내야하는 제출물을 보여줍니다."},
        			    	                 {name:"시상결과",href:"#contest_score",notice:"대회의 시상결과를 보여줍니다."}];
     						$scope.id=$scope.user.id;
     						if($location.search().team){
     						var team_id=$location.search().team;
     						if(team_id){
     							for(var i in $scope.team ){
     								if($scope.team[i].team_id==team_id){
     									$scope.team_id=$scope.team[i].team_id;
     									$scope.select($scope.liItems[2]);
     									
     								}
     							}
     						}
     						}
     						else{
     							$scope.select($scope.liItems[0]);
     						}
     						
     						
     						})
     
     				}
     				
     				
     			}
     	
     	)
     	
     }
     $scope.showModify=false;
     $scope.showCount=1;
     $scope.mo_textarea2=null;
     $scope.notice=null;
     
     $scope.notice_modify=function(){
    	 $('iframe').remove();
    	 
    	 $scope.showModify= ($scope.showModify==true ? false : true);
    		
				 nhn.husky.EZCreator.createInIFrame({
				      oAppRef: oEditors,
				      elPlaceHolder: "textarea2",
				      sSkinURI: "../se2/SmartEditor2Skin.html",        
				      fCreator: "createSEditor2"
				    	  
				    	  
				    	  
				 })
		 $scope.mo_textarea2=$scope.content.content;	 
    	 $scope.notice = $scope.content.notice==0 ? 0 : 1;

     }
     
     $scope.notice_delete=function(){
    	 
    	 myUtil.onHttp($http,'get','delete.do',{content_id:content_id,team_id:$scope.team_id},null,function(data){
    		 $scope.select($scope.liItems[0]);
		 })

     }
     $scope.file_delete=function(index){
    	if(confirm("정말 삭제하시겠습니까?")){
    	 myUtil.onHttp($http,'get','fileDelete.do',{file_id:$scope.files[index].file_id},null,function(data){
    		 if(data=='false'){
    			 alert("삭제 실패!! 다시시도해주세요");
    		 }
    		 else{
    			 $scope.files.splice(index,1);
    		 }
    	})
    	}

     }
     
     $scope.showWrite=false;
     $scope.notice_write=function(){
    	 $scope.showWrite= ($scope.showWrite==true ? false : true);
    	  
 
     }
     
     $scope.contest_board_board=function(content_id){
    	 $scope.content_id=content_id;
    		 myUtil.onHttp($http,'get','content2.do',{content_id:content_id,team_id: $scope.team_id},null,function(data){
    			 	$scope.content=data.content;
    			 	$scope.reply2=data.reply;
    			 	$scope.files=data.files;
    		 })
    		 
    		 if($scope.team_id){
    			 myUtil.onHttp($http,'get','content3.do',{content_id:content_id,team_id:$scope.team_id},null,function(data){
    			 		$scope.teamfiles=data;
    			 })
    		 }
     }
     
     $scope.team_board_board=function(content_id){
    	 $scope.content_id=content_id;
    		 myUtil.onHttp($http,'get','content2.do',{content_id:content_id,team_id: $scope.team_id},null,function(data){
    			 	$scope.content=data.content;
    			 	$scope.reply2=data.reply;
    			 	$scope.files=data.files;
    		 })
     }
     
     
     $scope.reportModify=function(index){
    	 $scope.r_modify=($scope.r_modify==index ? -1 : index);
    	 $scope.r_value=($scope.r_modify==index ? $scope.reportList[index].regdata_name : null);
     }
     
     
     $scope.r_complete=function(index){
    	 if($scope.reportList[index].regdata_name!=$scope.r_value || !$scope.r_value){
    		 var regdata_name=$scope.r_value;
    		 myUtil.onHttp($http,'get','reportComplete.do',{regdata_id:$scope.reportList[index].regdata_id,regdata_name:regdata_name,contest_reg_id:$scope.contest_reg_id},null,function(data){
    			 if(data){
    				 $scope.reportList[index]=data;
    			 }
    			 else{
    				 alert("이름이 중복되어 사용 할 수 없습니다.");
    			 }
    		 })
    	 }
    	 else{
    		 alert("이름이 변경되지 않았거나 공백입니다.");
    	 }
    	 $scope.reportModify();
     }
     
     $scope.r_delete=function(index){
    	 if(confirm("정말 삭제하시겠습니까?")){
    		 myUtil.onHttp($http,'get','reportDelete.do',{regdata_id:$scope.reportList[index].regdata_id},null,function(data){
    			 if(data=='true'){
    				 $scope.reportList.splice(index,1);
    			 }
    			 else{
    				 alert("이미 제출한 팀이 있어서 삭제 할 수 업습니다.");
    			 }
    		 })
    	 }
     }
     
     $scope.r_add=function(){
    	 myUtil.onHttp($http,'get','reportAdd.do',{regdata_name:$scope.regdata,contest_reg_id:$scope.contest_reg_id},null,function(data){
			 if(data){
				 $scope.reportList.push(data);
			 }
			 else{
				 alert("이름이 중복되거나 공백이어서 추가 할 수 없습니다.");
			 }
		 })
     }
     
     $scope.teamChange=function(team_id){
    	 
    	 $scope.team_id=team_id;
    	 $scope.select($scope.liItems[0]);
     }

     $scope.loadData();
})